# Copyright (C) 2025 Kumo inc.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

"""Tests -v/--verbosity flag and logging.root level's sync behavior."""

import logging

assert logging.root.getEffectiveLevel() == logging.WARN, (
    'default logging.root level should be WARN, but found {}'.format(
        logging.root.getEffectiveLevel()))

# This is here to test importing logging won't change the level.
logging.root.setLevel(logging.ERROR)

assert logging.root.getEffectiveLevel() == logging.ERROR, (
    'logging.root level should be changed to ERROR, but found {}'.format(
        logging.root.getEffectiveLevel()))

# pylint: disable=g-import-not-at-top
from turbo import flags
from turbo import logging as _  # pylint: disable=unused-import
from turbo.testing import turbotest
# pylint: enable=g-import-not-at-top

FLAGS = flags.FLAGS

assert FLAGS['verbosity'].value == -1, (
    '-v/--verbosity should be -1 before flags are parsed.')

assert logging.root.getEffectiveLevel() == logging.ERROR, (
    'logging.root level should be kept to ERROR, but found {}'.format(
        logging.root.getEffectiveLevel()))


class VerbosityFlagTest(turbotest.TestCase):

  def test_default_value_after_init(self):
    self.assertEqual(0, FLAGS.verbosity)
    self.assertEqual(logging.INFO, logging.root.getEffectiveLevel())


if __name__ == '__main__':
  turbotest.main()
